home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / bs941029.tgz / bbsx-941029.tar / bbsx / transfer.c < prev    next >
C/C++ Source or Header  |  1994-10-29  |  3KB  |  144 lines

  1. #ifndef __lint
  2. static char rcsid[] = "@(#) $Header: /home/dg1rtf/tcp/bbsx/RCS/transfer.c,v 1.2 1994/06/07 05:57:12 dg1rtf Exp $";
  3. #endif
  4.  
  5. #define _HPUX_SOURCE
  6.  
  7. #include <stdio.h>
  8.  
  9. #include <fcntl.h>
  10. #include <pwd.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/time.h>
  15. #include <sys/syslog.h>
  16.  
  17. #include "bbs.h"
  18. #include "bbs.hd"
  19.  
  20. extern char *optarg;
  21. extern int optint;
  22. const struct cmdtable cmdtable[1];
  23.  
  24.  
  25. /*---------------------------------------------------------------------------*/
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.  
  30.   static int fdindex;
  31.   char  src[80], dest[80];
  32.   char *p;
  33.   struct index index;
  34.   int c,signif;
  35.   int err_flag =0;
  36.   int delete =0, timestamp =0, at=0;
  37.   int count=0;
  38.   struct passwd *pw;
  39.   int i=1;
  40.   
  41.   if (getuid()) {
  42.     pw = getpwnam(bbsadm);
  43.     if(getuid() != pw->pw_uid) {
  44.       perror("permission denied");
  45.       return 1;
  46.     }
  47.   }    
  48.  
  49.   if (chdir(WRKDIR)) {
  50.     mkdir(WRKDIR, 0755);
  51.     if(chdir(WRKDIR)) halt();
  52.   }  
  53.  
  54.   read_config();
  55.  
  56.   if ((fdindex = open(INDEXFILE, O_RDWR, 0644)) < 0) goto stop;
  57.   while ((c = getopt(argc, argv, "d:at")) != EOF) {
  58.     switch (c) {
  59.       case 'd':
  60.         delete=1;
  61.         strcpy(src,optarg);
  62.         break;
  63.       case 't':
  64.         timestamp=1;
  65.         i++;
  66.         break;
  67.       case 'a':
  68.         at=1;
  69.         i++;
  70.         break;    
  71.       case '?':
  72.         err_flag=1;
  73.         break;
  74.       default:
  75.         err_flag=1;
  76.         break;
  77.     }
  78.   }  
  79.   if (argc < 3) err_flag = 1;
  80.   if (err_flag) {
  81.     puts("usage: transfer [-a] [-t] [-d] source [destination]");
  82.     exit(1);
  83.   }
  84.   
  85.   if (!delete) {
  86.     strcpy(src,argv[i]);strcpy(dest,argv[i+1]);
  87.     if (strlen(src) > 8)
  88.       src[8]= 0;
  89.     if (strlen(dest) > 8)
  90.       dest[8]= 0;
  91.   }
  92.  
  93.   for (; ; ) {
  94.     if (read(fdindex, (char *) &index, sizeof(index)) != sizeof(index)) goto stop;
  95.     if (!(index.flags & DELETED)) {
  96.       strupc(src);strupc(dest);
  97.       p=strchr(src,'*');
  98.       if (p)
  99.          signif = p-src;
  100.       else  
  101.         signif = (strlen(src) > strlen(index.to)) ? strlen(src) : strlen(index.to); 
  102.       if (signif > 8)
  103.         signif = 8;     
  104.       if (!strncmp(index.to,src,signif)) {
  105.         count++;
  106.         if (delete) {
  107.           unlink(getfilename(index.mesg));
  108.           index.flags |= DELETED;
  109.           if (lseek(fdindex, -sizeof(struct index), SEEK_CUR) < 0) return -1;
  110.           if (write(fdindex, &index, sizeof(struct index)) !=
  111.                     sizeof(struct index))
  112.             return -1;
  113.         }
  114.         else {
  115.           strcpy(index.to,dest);
  116.           if (lseek(fdindex, -sizeof(struct index), SEEK_CUR) < 0) return -1;
  117.           if (index.to[1] == 0) {
  118.             if (timestamp) {
  119.               index.date = time((long *) 0);
  120.             }  
  121.             if (at) { 
  122.               strcpy(index.at,Myhostname);
  123.             }  
  124.           } else
  125.             getalias(index.to);
  126.           if (write(fdindex, &index, sizeof(struct index)) != sizeof(struct index))
  127.           return -1;
  128.         }
  129.       }  
  130.     }
  131.   }
  132. stop:
  133.   close(fdindex);
  134.   
  135.   if (delete) {
  136.     printf("%d %s\n",count," messages deleted") ;
  137.   }   
  138.   else {
  139.     printf("%d %s\n",count," messages transfered");
  140.   }   
  141.   return 0;
  142. }
  143.  
  144.